package com.mzj.saas.config.datasource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {
		"com.mzj.saas" })
public class JPADataSourceConfig {

	@Value("${spring.datasource.primary.hibernate.show_sql}")
	private Boolean showSql;
	@Value("${spring.datasource.primary.org.hibernate.dialect}")
	private String hibernateDialet;

	@Autowired
	private JpaProperties jpaProperties;

	@Autowired
	@Qualifier("DataSource")
	private DataSource dataSource;

	@Bean(name = "entityManagerPrimary")
	@Primary
	public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
		return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
	}

	@PersistenceContext(unitName = "primary")
	@Bean(name = "entityManagerFactoryPrimary")
	@Qualifier("entityManagerFactoryPrimary")
	@Primary
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
		LocalContainerEntityManagerFactoryBean entityManagerFactory = builder.dataSource(dataSource)
				.properties(getVendorProperties(dataSource)).packages("com.mzj.saas").persistenceUnit("primary").build();
		return entityManagerFactory;
	}

	private Map<String, Object> getVendorProperties(DataSource dataSource) {
		HibernateSettings hibernateSettings = new HibernateSettings();
		// Map<String, String> jpaMap = jpaProperties.
		// jpaMap.put("hibernate.show_sql", showSql.toString());
		// jpaMap.put("hibernate.dialect", hibernateDialet);
		// jpaMap.put("hibernate.hbm2ddl.auto", "validate");
		return jpaProperties.getHibernateProperties(hibernateSettings);
	}

	@Bean(name = "transactionManagerPrimary")
	@Primary
	PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
		return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
	}

	@Bean(name = "jdbcTemplate")
	public JdbcTemplate getJdbcTemplate() {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		return jdbcTemplate;
	}

}
